package com.cet.programmercarl.algorithmperformancanalysis.二叉树.层级遍历;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @program: algorithm
 * @description: 填充每一个节点的下一节点指针,每层以#结尾
 * @author: 陈恩涛
 * @create: 2022-07-18 15:59
 **/
public class LC116 {

    public static void main(String[] args) {

    }

    public Node1 connect(Node1 root) {
        if (root == null) {
            return null;
        }
        final Queue<Node1> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int len = queue.size();
            while (len > 0) {
                final Node1 cur = queue.poll();
                if (len != 1) {
                    cur.next = queue.peek();
                }
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
                len--;
            }
        }
        return root;
    }
}

class Node1 {
    public int val;
    public Node1 left;
    public Node1 right;
    public Node1 next;

    public Node1() {}

    public Node1(int _val) {
        val = _val;
    }

    public Node1(int _val, Node1 _left, Node1 _right, Node1 _next) {
        val = _val;
        left = _left;
        right = _right;
        next = _next;
    }
}
